package com.dianban.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName：DateUtils @Description：
 * @author：yangs
 * @date：2015年8月22日 下午5:12:05
 */
public class DateUtils {

    public static final String Y_M_D = "yyyy-MM-dd";

    public static final String Y_M_D_H_M_S = "yyyy-MM-dd HH:mm:ss";

    public static final String YMD = "yyyyMMdd";

    public static final String YM = "yyyyMM";

    public static final String YMDHMS = "yyyyMMddHHmmss";

    public static final String YMDHMSS = "yyyyMMddHHmmssSSS";

    public static final String SLASH_DISPLAY_DATE_FULL = "yyyy/MM/dd HH:mm:ss";

    public static final String POINT_DISPLAY_YMD = "yyyy.MM.dd";


    public static String formatNow(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(new Date());
    }

    public static String format(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static Date parse(String date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param date
     * @param pattern
     * @return Date
     * @Title：parseDate
     * @Description：格式化时间，返回时间格式
     */
    public static Date parseDate(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param year
     * @return Date
     * @Title：getEndTime @Description：获取每年最后时间，比如2015-12-31 23:59:59
     */
    public static Date getEndTime(int year) {
        Calendar c = Calendar.getInstance();
        c.set(year + 1, 0, 1, 0, 0, 0);
        c.setTimeInMillis(c.getTimeInMillis() - 1000);
        return c.getTime();
    }

    /**
     * @param year
     * @return Date
     * @Title：getStartTime @Description：获取每年开始时间，比如2015-1-1 00:00:00
     */
    public static Date getStartTime(int year) {
        Calendar c = Calendar.getInstance();
        c.set(year, 0, 1, 0, 0, 0);
        return c.getTime();
    }

    /**
     * @param date
     * @return Map<String, String>
     * @Title：getDateMap
     * @Description：获取时间年月日，时分秒的Map
     */
    public static Map<String, Integer> getDateMap(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        Map<String, Integer> dateMap = new HashMap<String, Integer>();
        dateMap.put("year", c.get(Calendar.YEAR) + 1900);
        dateMap.put("month", c.get(Calendar.MONTH) + 1);
        dateMap.put("date", c.get(Calendar.DATE));
        dateMap.put("hour", c.get(Calendar.HOUR_OF_DAY));
        dateMap.put("minute", c.get(Calendar.MINUTE));
        dateMap.put("second", c.get(Calendar.SECOND));
        return dateMap;
    }

    /**
     * 获取当前日期是星期几<br>
     *
     * @param dt
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * @param date
     * @return Map<String, String>
     * @Title：getDateMap
     * @Description：获取时间年月日，时分秒的Map
     */
    public static Map<String, Long> getDifftimeMap(Date date1, Date date2) {
        Map<String, Long> dateMap = new HashMap<String, Long>();
        Long l = date2.getTime() - date1.getTime();
        Long day = l / (24 * 60 * 60 * 1000);
        Long hour = (l / (60 * 60 * 1000) - day * 24);
        Long minute = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
        Long second = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60);
        dateMap.put("day", day);
        dateMap.put("hour", hour);
        dateMap.put("minute", minute);
        dateMap.put("second", second);
        return dateMap;
    }

    /**
     * 计算日期A和日期B天数之差，负数为小几天
     *
     * @param dateA
     * @param dateB
     * @return
     * @throws ParseException
     */
    public static int differDay(Date dateA, Date dateB) throws ParseException {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String dateAStr = f.format(dateA);
        String dateBStr = f.format(dateB);

        dateA = f.parse(dateAStr);
        dateB = f.parse(dateBStr);

        int subDay = (int) ((dateA.getTime() - dateB.getTime()) / (1000 * 60 * 60 * 24));// 计算几天
        return subDay;
    }

    /**
     * 计算日期A和日期B天数之差，负数为小几天
     *
     * @param dateA
     * @param dateB
     * @return
     * @throws ParseException
     */
    public static int differMinute(Date dateA, Date dateB) throws ParseException {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateAStr = f.format(dateA);
        String dateBStr = f.format(dateB);

        dateA = f.parse(dateAStr);
        dateB = f.parse(dateBStr);

        int subDay = (int) ((dateA.getTime() - dateB.getTime()) / (1000 * 60));// 计算几天
        return subDay;
    }

    /**
     * 对日期时间对象进行调整
     *
     * @param date
     * @param CALENDARFIELD <pre>
     *                                           年 Calendar.YEAR
     *                                           月 Calendar.MONTH
     *                                           日 Calendar.DATE
     *                                           时 Calendar.HOUR
     *                                           分 Calendar.MINUTE
     *                                           秒 Calendar.SECOND
     *                                                      </pre>
     * @param amount        调整数量，>0表向后调整（明天，明年），<0表向前调整（昨天，去年）
     * @return
     * @author:chenlf
     */
    public static Date transpositionDate(Date date, int CALENDARFIELD, int amount) {
        if (null == date) {
            return date;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(CALENDARFIELD, amount);
        return calendar.getTime();
    }

    public static Date setStartDay(Calendar cal) {
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    public static Date setEndDay(Calendar cal) {
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    public static Date addYears(Date date, int amount) {
        return add(date, 1, amount);
    }

    public static Date addMonths(Date date, int amount) {
        return add(date, 2, amount);
    }

    public static Date addWeeks(Date date, int amount) {
        return add(date, 3, amount);
    }

    public static Date addDays(Date date, int amount) {
        return add(date, 5, amount);
    }

    public static Date addHours(Date date, int amount) {
        return add(date, 11, amount);
    }

    public static Date addMinutes(Date date, int amount) {
        return add(date, 12, amount);
    }

    public static Date addSeconds(Date date, int amount) {
        return add(date, 13, amount);
    }

    public static Date addMilliseconds(Date date, int amount) {
        return add(date, 14, amount);
    }

    private static Date add(Date date, int calendarField, int amount) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        } else {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(calendarField, amount);
            return c.getTime();
        }
    }

    /**
     * @Title: getLastDate @Description: TODO(传入CurrDate时间的第day天) @param CurrDate @param day @return @throws
     */
    public static Date getLastDate(Date CurrDate, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(CurrDate);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + day);// 让日期加day
        Date lastDate = calendar.getTime();
        return lastDate;
    }

    // 每月第一天
    public static String generateMonthDayBegin() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date begin = calendar.getTime();
        return sdf.format(begin);
    }

    // 每月最后一天
    public static String generateMonthDayEnd() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date end = calendar.getTime();
        return sdf.format(end);
    }

    /**
     * 当天00:00:00
     * @return
     */
    public static String generateDayBegin() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date begin = calendar.getTime();
        return sdf.format(begin);
    }

    /**
     * 当天23:59.59
     * @return
     */
    public static String generateDayEnd() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date end = calendar.getTime();
        return sdf.format(end);
    }

}
